var salt = sessionStorage.getItem('usersalt');

layui.use(['form', 'layer', 'table'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		table = layui.table;
	
	//批量导出
	$(".export_btn").on("click", function(){
		var checkStatus = table.checkStatus('studyListTable'),
		data = checkStatus.data;
		var deleteIdArr = new Array();
		for(var i = 0; i < data.length; i++) {
			deleteIdArr.push(data[i].id);
		}
		if(data.length > 0) {
			$.post(url + 'course/exportInfo', {
				Token: salt,
				ids: deleteIdArr.toString()
			}, function(result) {
				window.open(url + "file" + result.data);
			});
		} else {
			layer.msg("请选择需要导出的数据");
		}
	});
	
	/*取出所有的单位*/
	var company;
	function getAllDanwei() {
		$.ajax({
			url: url + 'baseEnt/getAllEnt',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
			},
			success: function(data) {
				$('#danwei').empty();
				if(data.code == 0) {
					company = data.data;
				}

			},
			error: function(error) {
			}
		});
	}
	getAllDanwei();

	/*------------tree.start------------*/
    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onCheck: zTreeOnCheck
        }
    };
    var zNodes = company;
    $.fn.zTree.init($("#danwei"), setting, zNodes);

    function getSydwVal() {
        var treeArr = [];
        var treeObj = $.fn.zTree.getZTreeObj("danwei");
        var nodes = treeObj.getCheckedNodes(true);
        for(var i = 0; i < nodes.length; i++) {
            treeArr.push(nodes[i].id); //获取选中节点的值
        }
        return treeArr;
    }
    $('#resclas').click(function() {
        $("body").trigger("click");
        $('#danwei').show();


    });

    function zTreeOnCheck(event, treeId, treeNode) {
        var nval = "";
        var treeObj = $.fn.zTree.getZTreeObj("danwei");
        var nodes = treeObj.getCheckedNodes(true);
        for(var i = 0; i < nodes.length; i++) {
            nval += nodes[i].name + ",";
        }
        $('#resclas').val(nval.substr(0, nval.length - 1));
    };
	/*------------tree.end------------*/
	
	//筛选
	$("#selStudy").click(function(){
		getStudyList();
	});
	
	//获取表格数据
	function getStudyList(){
		table.render({
			elem: '#studyList',
			method: 'post',
			url: url + 'course/getStudyPage',
			where: {
				Token: salt, //用户码
				entId: getSydwVal().toString() //适用单位ids
			},
			cellMinWidth: 95,
			page: true,
			height: "full-115",
			id: "studyListTable",
			cols: [
				[
					{type: "numbers", width: 50, title: "序号"},
					{type: "checkbox", width: 50},
					{field: 'userName', title: '姓名', align: 'center', templet: function(p) {
							if (p.userName == null) {
								return "<div></div>";
							} else {
								return "<div onclick=showDetail('" + p.id + "')>" + p.userName + "</div>";
							}
						}
					},
					{field: 'entName', title: '所在单位', align: 'center', templet: function(p) {
							if (p.entName == null) {
								return "<div></div>";
							} else {
								return "<div onclick=showDetail('" + p.id + "')>" + p.entName + "</div>";
							}
						}
					},
					{field: 'duration', title: '学时统计', align: 'center', templet: function(p) {
							if (p.duration == null) {
								return "<div></div>";
							}
							return "<div onclick=showDetail('" + p.id + "')>" + p.duration + "分钟</div>";
						}
					}
				]
			],
			done: function(res, curr, count) {
			}
		});
	}
	getStudyList();

	window.showDetail = function(id){
		location.href = "../study-detail/study-detail.html?id=" + id;
	};
	
});
